package lj.leetcode.code134;

public class Main {
    public static void main(String[] args){
        int[] gas={2,3,4}, cost={3,4,3};
        Solution A=new Solution();
        System.out.println(A.canCompleteCircuit(gas,cost));
    }
}
class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int length = gas.length;
        int sum = 0, temp = 0, star = 0, sum2 = 0;
        for(int i = 0; i < length; i++){
            temp = i + 1 - cost[i];
            sum += temp;
            sum2 += temp;
            if(sum2 < 0){
                star = i+1;
                sum2 = 0;
            }
        }
        return sum<0?-1:star;
    }
}
